/// 工具类 `StrUtil` 用于处理字符串相关操作
pub struct StrUtil;

impl StrUtil {
    /// 检查字符串是否为空
    ///
    /// # 参数
    /// * `s` - 待检查的字符串
    ///
    /// # 返回值
    /// 如果字符串为空则返回 `true`，否则返回 `false`
    ///
    /// # 示例
    ///
    /// ```
    /// let empty_string = "";
    /// let result = StrUtil::is_empty(empty_string);
    /// assert_eq!(result, true);
    /// ```
    pub fn is_empty(s: &str) -> bool {
        s.is_empty()
    }

    /// 检查字符串是否不为空
    ///
    /// # 参数
    /// * `s` - 待检查的字符串
    ///
    /// # 返回值
    /// 如果字符串不为空则返回 `true`，否则返回 `false`
    ///
    /// # 示例
    ///
    /// ```
    /// let non_empty_string = "hello";
    /// let result = StrUtil::is_not_empty(non_empty_string);
    /// assert_eq!(result, true);
    /// ```
    pub fn is_not_empty(s: &str) -> bool {
        !Self::is_empty(s)
    }

    /// 检查字符串是否以指定的前缀开头
    ///
    /// # 参数
    /// * `s` - 待检查的字符串
    /// * `prefix` - 前缀字符串
    ///
    /// # 返回值
    /// 如果字符串以指定前缀开头则返回 `true`，否则返回 `false`
    ///
    /// # 示例
    ///
    /// ```
    /// let s = "hello world";
    /// let prefix = "hello";
    /// let result = StrUtil::starts_with(s, prefix);
    /// assert_eq!(result, true);
    /// ```
    pub fn starts_with(s: &str, prefix: &str) -> bool {
        s.starts_with(prefix)
    }

    /// 检查字符串是否以指定的后缀结尾
    ///
    /// # 参数
    /// * `s` - 待检查的字符串
    /// * `suffix` - 后缀字符串
    ///
    /// # 返回值
    /// 如果字符串以指定后缀结尾则返回 `true`，否则返回 `false`
    ///
    /// # 示例
    ///
    /// ```
    /// let s = "hello world";
    /// let suffix = "world";
    /// let result = StrUtil::ends_with(s, suffix);
    /// assert_eq!(result, true);
    /// ```
    pub fn ends_with(s: &str, suffix: &str) -> bool {
        s.ends_with(suffix)
    }

    /// 去除字符串两端的空白字符
    ///
    /// # 参数
    /// * `s` - 待处理的字符串
    ///
    /// # 返回值
    /// 返回去除两端空白字符后的新字符串
    ///
    /// # 示例
    ///
    /// ```
    /// let s = "   hello world!   ";
    /// let trimmed = StrUtil::trim(s);
    /// assert_eq!(trimmed, "hello world!");
    /// ```
    pub fn trim(s: &str) -> String {
        s.trim().to_owned()
    }

    /// 去除字符串开头的空白字符
    ///
    /// # 参数
    /// * `s` - 待处理的字符串
    ///
    /// # 返回值
    /// 返回去除开头空白字符后的新字符串
    ///
    /// # 示例
    ///
    /// ```
    /// let s = "   hello world!   ";
    /// let trimmed = StrUtil::trim_left(s);
    /// assert_eq!(trimmed, "hello world!   ");
    /// ```
    pub fn trim_left(s: &str) -> String {
        s.trim_start().to_owned()
    }

    /// 去除字符串结尾的空白字符
    ///
    /// # 参数
    /// * `s` - 待处理的字符串
    ///
    /// # 返回值
    /// 返回去除结尾空白字符后的新字符串
    ///
    /// # 示例
    ///
    /// ```
    /// let s = "   hello world!   ";
    /// let trimmed = StrUtil::trim_right(s);
    /// assert_eq!(trimmed, "   hello world!");
    /// ```
    pub fn trim_right(s: &str) -> String {
        s.trim_end().to_owned()
    }

    /// 将字符串重复指定次数
    ///
    /// # 参数
    /// * `s` - 待处理的字符串
    /// * `count` - 重复次数
    ///
    /// # 返回值
    /// 返回重复后的新字符串
    ///
    /// # 示例
    ///
    /// ```
    /// let s = "abc";
    /// let repeated = StrUtil::repeat(s, 3);
    /// assert_eq!(repeated, "abcabcabc");
    /// ```
    pub fn repeat(s: &str, count: usize) -> String {
        s.repeat(count)
    }

    /// 将字符串转换为小写
    ///
    /// # 参数
    /// * `s` - 待转换的字符串
    ///
    /// # 返回值
    /// 返回转换为小写后的新字符串
    ///
    /// # 示例
    ///
    /// ```
    /// let s = "HeLLo WoRLd";
    /// let lowercased = StrUtil::to_lowercase(s);
    /// assert_eq!(lowercased, "hello world");
    /// ```
    pub fn to_lowercase(s: &str) -> String {
        s.to_lowercase()
    }

    /// 将字符串转换为大写
    ///
    /// # 参数
    /// * `s` - 待转换的字符串
    ///
    /// # 返回值
    /// 返回转换为大写后的新字符串
    ///
    /// # 示例
    ///
    /// ```
    /// let s = "HeLLo WoRLd";
    /// let uppercased = StrUtil::to_uppercase(s);
    /// assert_eq!(uppercased, "HELLO WORLD");
    /// ```
    pub fn to_uppercase(s: &str) -> String {
        s.to_uppercase()
    }

    /// 将字符串中所有出现的指定模式替换为新的字符串
    ///
    /// # 参数
    /// * `s` - 原字符串
    /// * `from` - 需要替换的模式
    /// * `to` - 替换后的字符串
    ///
    /// # 返回值
    /// 返回替换后的新字符串
    ///
    /// # 示例
    ///
    /// ```
    /// let s = "hello world";
    /// let result = StrUtil::replace(s, "world", "Rust");
    /// assert_eq!(result, "hello Rust");
    /// ```
    pub fn replace(s: &str, from: &str, to: &str) -> String {
        s.replace(from, to)
    }

    /// 按照指定分隔符拆分字符串，并返回拆分后的子字符串向量
    ///
    /// # 参数
    /// * `s` - 待拆分的字符串
    /// * `sep` - 分隔符字符串
    ///
    /// # 返回值
    /// 返回拆分后的子字符串向量
    ///
    /// # 示例
    ///
    /// ```
    /// let s = "hello world";
    /// let parts = StrUtil::split(s, " ");
    /// assert_eq!(parts, vec!["hello", "world"]);
    /// ```
    pub fn split(s: &str, sep: &str) -> Vec<String> {
        s.split(sep).map(|s| s.to_owned()).collect()
    }

    /// 使用指定的分隔符连接字符串切片
    ///
    /// # 参数
    /// * `strings` - 字符串切片
    /// * `sep` - 分隔符字符串
    ///
    /// # 返回值
    /// 返回连接后的新字符串
    ///
    /// # 示例
    ///
    /// ```
    /// let strings = vec!["hello", "world"];
    /// let result = StrUtil::join(&strings, ", ");
    /// assert_eq!(result, "hello, world");
    /// ```
    pub fn join(strings: &[&str], sep: &str) -> String {
        strings.join(sep)
    }
}

fn main() {
    // 初始化日志
    env_logger::init();

    let s = "   hello world!   ";

    // 检查字符串是否为空
    log::info!("is_empty: {}", StrUtil::is_empty(""));

    // 检查字符串是否不为空
    log::info!("is_not_empty: {}", StrUtil::is_not_empty(s));

    // 检查字符串是否以指定前缀开头
    log::info!("starts_with: {}", StrUtil::starts_with(s, "hello"));

    // 检查字符串是否以指定后缀结尾
    log::info!("ends_with: {}", StrUtil::ends_with(s, "!"));

    // 去除字符串两端的空白字符
    log::info!("trim: '{}'", StrUtil::trim(s));

    // 去除字符串开头的空白字符
    log::info!("trim_left: '{}'", StrUtil::trim_left(s));

    // 去除字符串结尾的空白字符
    log::info!("trim_right: '{}'", StrUtil::trim_right(s));

    // 将字符串重复指定次数
    log::info!("repeat: '{}'", StrUtil::repeat("abc", 3));

    // 将字符串转换为小写
    log::info!("to_lowercase: '{}'", StrUtil::to_lowercase(s));

    // 将字符串转换为大写
    log::info!("to_uppercase: '{}'", StrUtil::to_uppercase(s));

    // 将字符串中所有出现的指定模式替换为新的字符串
    log::info!("replace: '{}'", StrUtil::replace(s, "world", "Rust"));

    // 按照指定分隔符拆分字符串
    log::info!("split: '{:?}'", StrUtil::split(s, " "));

    // 使用指定的分隔符连接字符串切片
    log::info!("join: '{}'", StrUtil::join(&["hello", "world"], ", "));
}